package com.hiwan.unions.activity.system;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.application.exception.ApiException;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.ToastUtils;

/**
 * Created by llg on 2016/3/25.
 * 反馈界面
 */
public class FeedbackActivity extends RootActivity {
    private EditText TextViewContent, TextViewContent_2;
    private EditText TextViewTitle;
    private TextView text_title;
    private LinearLayout layout_1;
    private boolean type = false;
    private String string1, string2;

    @Override
    protected int getLayout() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initData() {
        bundle = getIntent().getExtras();
        setTitleTxt(bundle.getString("typeString"));
        setOperationBtn("提交");
        if (bundle.getString("typeString").equals("法律咨询")) {
            text_title.setText("问题描述");
            layout_1.setVisibility(View.VISIBLE);
            type = true;
        }

    }

    public static void startPreview(Context context, String type) {
        Intent intent = new Intent(context, FeedbackActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("typeString", type);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }


    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        text_title = findView(R.id.text_title);
        layout_1 = findView(R.id.layout_1);
        TextViewContent = findView(R.id.TextViewContent);
        TextViewContent_2 = findView(R.id.TextViewContent_2);
        TextViewTitle = findView(R.id.TextViewTitle);
//        textA = findView(R.id.textA);
        edit(TextViewContent);
        edit(TextViewContent_2);
        TextViewContent.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            //输入表情前的光标位置
            private int cursorPos;
            //输入表情前EditText中的文本
            private String inputAfterText;
            //是否重置了EditText的内容
            private boolean resetText;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                if (!resetText) {
                    cursorPos = TextViewContent.getSelectionEnd();
                    // 这里用s.toString()而不直接用s是因为如果用s，
                    // 那么，inputAfterText和s在内存中指向的是同一个地址，s改变了，
                    // inputAfterText也就改变了，那么表情过滤就失败了
                    inputAfterText = s.toString();
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                try {
                    temp = s;
                    if (!resetText) {
                        if (count >= 2) {//表情符号的字符长度最小为2
                            CharSequence input = s.subSequence(cursorPos, cursorPos + count);
                            if (CheckUtils.containsEmoji(input.toString())) {
                                resetText = true;
                                //是表情符号就将文本还原为输入表情符号之前的内容
                                TextViewContent.setText(inputAfterText);
                                CharSequence text = TextViewContent.getText();
                                if (text instanceof Spannable) {
                                    Spannable spanText = (Spannable) text;
                                    Selection.setSelection(spanText, text.length());
                                }
                            }
                        }
                    } else {
                        resetText = false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
//                int i = 500 - temp.length();
//                textA.setText("您还可输入 " + i + " 个字");
//                if (temp.length() == 500) {
//                    textA.setText("最多可输入500个字");
//                }

            }
        });
    }

    private void edit(EditText e) {
        e.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });
    }

    @Override
    public void getOperationBtn() {
        super.getOperationBtn();
        try {
            if (type) {
                string1 = CheckUtils.check(TextViewContent, "请输入问题描述");
                string2 = CheckUtils.check(TextViewContent_2, "请输入援助需求");
            } else {
                string1 = CheckUtils.check(TextViewContent, "请输入内容");
            }
        } catch (ApiException e) {
            ToastUtils.showToast(this, e.getMessage());
            return;
        }

        if (CheckUtils.isEmpty(TextViewContent)) {
            ToastUtils.showToast(this, "请输入投诉建议内容");
        } else {
            ToastUtils.showToast(FeedbackActivity.this, "已提交，我们会尽快处理");
            finish();
        }
    }
}
